﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Widgetsphere.Autos.Websitev2.Objects
{
	internal class BasePageHelper
	{
		/// <summary>
		/// This will move view state to the bottom of the page. 
		/// Full article is located http://professionalaspnet.com/blogs/aspnet_blog/archive/2006/07/30/Moving-ViewState-to-Optimize-Web-Pages.aspx
		/// </summary>
		/// <param name="writer">HtmlTextWriter object containing text to be
		/// rendered to the page</param>
		public static string RelocateViewState(BasePage page, string html)
		{
			//Rearrange the VIEWSTATE
			var StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
			if (StartPoint >= 0)
			{
				var EndPoint = html.IndexOf("/>", StartPoint) + 4;
				var viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint);
				html = html.Remove(StartPoint, EndPoint - StartPoint);
				var FormEndStart = html.IndexOf("</form>");
				if (FormEndStart >= 0)
				{
					//In some cases the space between "</form>" will be missing so insert one
					html = html.Insert(FormEndStart, " ");

					//Then Insert ViewState Section
					html = html.Insert(FormEndStart, viewstateInput);
				}
			}

			//Rearrange the EVENTTARGET
			StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__EVENTTARGET\"");
			if (StartPoint >= 0)
			{
				var EndPoint = html.IndexOf("/>", StartPoint) + 4;
				var viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint);
				html = html.Remove(StartPoint, EndPoint - StartPoint);
				var FormEndStart = html.IndexOf("</form>");
				if (FormEndStart >= 0)
				{
					//In some cases the space between "</form>" will be missing so insert one
					html = html.Insert(FormEndStart, " ");

					//Then Insert ViewState Section
					html = html.Insert(FormEndStart, viewstateInput);
				}
			}

			//Rearrange the EVENTTARGET
			StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__EVENTARGUMENT\"");
			if (StartPoint >= 0)
			{
				var EndPoint = html.IndexOf("/>", StartPoint) + 2;
				var viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint);
				html = html.Remove(StartPoint, EndPoint - StartPoint);
				var FormEndStart = html.IndexOf("</form>");
				if (FormEndStart >= 0)
				{
					//In some cases the space between "</form>" will be missing so insert one
					html = html.Insert(FormEndStart, " ");

					//Then Insert ViewState Section
					html = html.Insert(FormEndStart, viewstateInput);
				}
			}

			//Rearrange the TITLE
			StartPoint = html.IndexOf("<title");
			if (StartPoint >= 0)
			{
				var EndPoint = html.IndexOf("</title>", StartPoint) + 8;
				var viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint);
				html = html.Remove(StartPoint, EndPoint - StartPoint);
				var FormEndStart = html.IndexOf("<head>");
				if (FormEndStart >= 0)
				{
					//Then Insert ViewState Section
					html = html.Insert(FormEndStart + 6, "\n" + viewstateInput);
				}
			}

			// Use the same method to move the javascript blocks inserted by .Net to the bottom too
			StartPoint = html.IndexOf("<script type=\"text/javascript\">\r\n<!--\r\nvar theForm = document.forms[\'aspnetForm\']");
			if (StartPoint >= 0)
			{
				var EndPoint = html.IndexOf("</script>", StartPoint) + 11;
				var scriptInput = html.Substring(StartPoint, EndPoint - StartPoint);
				html = html.Remove(StartPoint, EndPoint - StartPoint);
				var FormEndStart = html.IndexOf("</form>");
				if (FormEndStart >= 0)
				{
					//In some cases the space between "</form>" will be missing so insert one
					html = html.Insert(FormEndStart, " ");

					//Then Insert ViewState Section
					html = html.Insert(FormEndStart, scriptInput);
				}
			}

			//how many scripts are we going to move
			//int scriptCount = 0;
			//StartPoint = html.IndexOf("<script src=\"/WebResource.axd");
			//while (StartPoint >= 0)
			//{
			//  scriptCount++;
			//  StartPoint = html.IndexOf("<script src=\"/WebResource.axd", StartPoint + 1);
			//}

			////Now move the scripts
			//StartPoint = html.IndexOf("<script src=\"/WebResource.axd");
			//for (int ii = 0; ii < scriptCount; ii++)
			//{
			//  int EndPoint = html.IndexOf("</script>", StartPoint) + 9;
			//  string scriptInput = html.Substring(StartPoint, EndPoint - StartPoint);
			//  html = html.Remove(StartPoint, EndPoint - StartPoint);
			//  int FormEndStart = html.IndexOf("</form>");
			//  if (FormEndStart >= 0)
			//  {
			//    //In some cases the space between "</form>" will be missing so insert one
			//    html = html.Insert(FormEndStart, " ");

			//    //Then Insert ViewState Section
			//    html = html.Insert(FormEndStart, scriptInput);
			//  }
			//  StartPoint = html.IndexOf("<script src=\"/WebResource.axd", StartPoint + 1);
			//}

			//Look for and move the WebForm_OnSubmit function
			StartPoint = html.IndexOf("<script type=\"text/javascript\">\r\n<!--\r\nfunction WebForm_OnSubmit");
			if (StartPoint >= 0)
			{
				var EndPoint = html.IndexOf("</script>", StartPoint) + 11;
				var scriptInput = html.Substring(StartPoint, EndPoint - StartPoint);
				html = html.Remove(StartPoint, EndPoint - StartPoint);
				var FormEndStart = html.IndexOf("</form>");
				if (FormEndStart >= 0)
				{
					//In some cases the space between "</form>" will be missing so insert one
					html = html.Insert(FormEndStart, " ");

					//Then Insert ViewState Section
					html = html.Insert(FormEndStart, scriptInput);
				}
			}

			//html = BasePageHelper.AddGoogle(HttpContext.Current.Request, html);

			return html;
		}
	}
}